<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="application/x-blueprint+xml;charset=UTF-8" language="java" %>
<%@ page import="com.vhly.bpplat.twitter.HTTPClient" %>
<%@ page import="com.vhly.bpplat.twitter.model.TwitterParser" %>
<%@ page import="com.vhly.bpplat.twitter.model.User" %>
<%@ page import="org.kxml2.io.KXmlParser" %>
<%@ page import="org.kxml2.kdom.Document" %>
<%@ page import="org.xmlpull.v1.XmlPullParserException" %>
<%@ page import="java.io.ByteArrayInputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Vector" %>
<page xmlns:xsd="http://mobile.yahoo.com/widgets/schema/1.5/blueprint/">
    <models>
        <model id="login_info">
            <instance>
                <data xmlns="">
                    <n/>
                </data>
            </instance>
            <submission method="urlencoded-post" resource="search.jsp"/>
        </model>
    </models>
    <content>
        <module>
            <header layout="simple">
                <layout-items>
                    <block class="title">Search Friends</block>
                </layout-items>
            </header>
            <input ref="n">
                <label>People Name</label>
            </input>
            <submit>
                <label>Search</label>
            </submit>
            <%
                Object login = session.getAttribute("login");
                if (login != null) {
                    String name = request.getParameter("n");
                    int currentPage = 1;
                    String pn = request.getParameter("p");
                    if (name != null && name.length() > 0) {
                        StringBuffer sb = new StringBuffer("http://twitter.com/users/search.xml?q=");
                        sb.append(URLEncoder.encode(name, "UTF-8"));
                        if (pn != null && pn.length() > 0) {
                            sb.append("&page=").append(pn);
                            currentPage = Integer.parseInt(pn);
                        }
                        String user = (String) session.getAttribute("user");
                        String pass = (String) session.getAttribute("pass");
                        String acc = user + ":" + pass;
                        String xml = HTTPClient.request(sb.toString(), acc, null, false);
                        if (xml != null) {
//                            System.out.println("Search xml = " + xml);
                            Document dom = new Document();
                            KXmlParser parser = new KXmlParser();
//                            parser.defineEntityReplacementText("&amp;"," &amp;");
//                            System.out.println("xml = " + xml);
                            byte[] buf = xml.getBytes("UTF-8");
                            ByteArrayInputStream bin = null;
                            Vector<User> users = null;
                            try {
                                bin = new ByteArrayInputStream(buf);
                                parser.setInput(bin, null);
                                dom.parse(parser);
                                users = TwitterParser.parseUsers(dom.getRootElement());
                                if (!users.isEmpty()) {
                                    for (User usr : users) {
                                        usr.description = usr.description.replaceAll("&", "&amp;");
            %>
            <module>
                <header layout="simple">
                    <layout-items>
                        <block class="title"><%=usr.screen_name%></block>
                    </layout-items>
                </header>
                <placard layout="card">
                    <layout-items>
                        <image resource="<%=usr.image_url%>"/>
                        <block class="title"><%=usr.name%></block>
                        <block class="description"><%=usr.description%></block>
                        <block class="subtext"><%=usr.url%></block>
                        <block class="subtext"><%=usr.created_at%></block>
                        <block class="subtext">Friends(<%=usr.friends_count%>)</block>
                        <%
                            if (usr.lastStatus != null) {
                        %>
                        <block class="subtext">Last Status: <%=usr.lastStatus.text%></block>
                        <%
                            }
                        %>
                    </layout-items>
                </placard>
                <trigger>
                    <label>Follow</label>
                    <load event="activate" resource="follow.jsp?t=f&amp;u=<%=usr.id%>"/>
                </trigger>
            </module>
            <%
                }

            %>

            <navigation-bar>
                <%if (currentPage > 1) {%>
                <prev>
                    <label>Prev</label>
                    <load event="activate"
                          resource="search.jsp?n=<%=(URLEncoder.encode(name,"UTF-8"))%>&amp;p=<%=(currentPage - 1)%>"/>
                </prev>
                <%
                    }
                %>
                <next>
                    <label>Next</label>
                    <load event="activate"
                          resource="search.jsp?n=<%=(URLEncoder.encode(name,"UTF-8"))%>&amp;p=<%=(currentPage + 1)%>"/>
                </next>
            </navigation-bar>

            <%
            } else {
            %>
            <block>Not Found by keyword "<%=name%>"</block>
            <%
                    }
                } catch (XmlPullParserException xe) {

                } finally {
                    if (users != null) {
                        users.clear();
                    }
                    buf = null;
                    xml = null;
                    parser = null;
                    try {
                        bin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    bin = null;
                }
            } else {
            %>
            <block>Find Error</block>

            <%


                    }
                }
            } else {
            %>
            <block>Need Login</block>
            <%
                }
            %>
        </module>
    </content>
</page>